import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:get/get.dart';
import 'package:get/state_manager.dart';

import '../../bean/bean_study_data.dart';
import 'controller/study_data_controller.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/13.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class StudyDataItemPage extends StatefulWidget {
  const StudyDataItemPage({Key key}) : super(key: key);

  @override
  _StudyDataPageState createState() => _StudyDataPageState();
}

///当前页面被装载在[PageView]中，使用KeepAlive使用页面保持状态
class _StudyDataPageState extends State<StudyDataItemPage> {

  TabController _tabController;
  @override
  void initState() {
    super.initState();
    Get.put(MyStudyDataController());

    Future.delayed(Duration.zero, () {
      Get.find<MyStudyDataController>().onPullRefresh();
    });
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(child: GetBuilder(
      builder: (MyStudyDataController controller) {
        return buildManiWidget(controller);
      },
    ), onRefresh: () async {
      return Get.find<MyStudyDataController>().onPullRefresh();
    });
  }

  buildManiWidget(MyStudyDataController controller) {
    if (controller.isLoading) {
      return LoadingWidget();
    }

    List<MaterialCategoryBean> studyDataList = controller.materialCategoryList;
    if (studyDataList.length == 0) {
      return NoDataWidget(
        clickCallBack: () {
          Get.find<MyStudyDataController>().onClickRefresh();
        },
      );
    }

    return buildListWidget(null);
  }

  buildListWidget(List<StudyDataBean> studyDataList) {
    return ListView.builder(
      itemCount: studyDataList.length,
      itemBuilder: (BuildContext context, int index) {
        StudyDataBean item = studyDataList[index];
        bool isShowTitle = true;
        bool isShowBottomTitle = true;
        int type = item.videoTypes;
        Color textColor = Colors.grey;
        String preText = "";
        if (type == 1) {
          textColor = Colors.red;
          preText = "+";
        } else if (type == 2) {
          textColor = Colors.black;
          preText = "-";
        } else if (type == 2) {
          textColor = Colors.black;
          preText = "-";
        }
        return Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(left: 12, top: 12, right: 12),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text(
                                  item.videoName,
                                  style: TextStyle(
                                      fontSize: 14, color: Colors.black),
                                ),
                                SizedBox(
                                  height: 8,
                                ),
                                Text(
                                  item.videoName,
                                  style: TextStyle(
                                      fontSize: 12, color: Color(0xff666666)),
                                )
                              ],
                            ),
                          ),
                          Text(
                            "$preText ${item.videoName}",
                            style: TextStyle(
                                color: textColor,
                                fontSize: 16,
                                fontWeight: FontWeight.w600),
                          )
                        ],
                      ),
                      Container(
                        width: double.infinity,
                        height: 1,
                        color: Color(0xffeeeeee),
                        margin: EdgeInsets.only(top: 10),
                      )
                    ],
                  ))
            ],
          ),
        );
      },
    );
  }
}
